//
//  GYMineShopAddressEditViewModel.m
//  GYDemo
//
//  Created by mac on 2018/6/20.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYMineShopAddressEditViewModel.h"
#import "Head.h"

@implementation GYMineShopAddressUpdateModel
#pragma mark - getter and setter
#pragma mark - method
#pragma mark - init
-(instancetype)init
{
    if (self = [super init]) {
        [self setTitleInfo:@{
                             @"province":@"所在地区",
                             @"city":@"所在地区",
                             @"area":@"所在地区",
                             @"addr":@"详细地址",
                             @"name":@"收货人姓名",
                             @"mobile":@"联系电话"
                             }];
        [self setMustKeys:@[
                            @"province",@"city",@"area",@"addr",@"postalcode",@"name",@"mobile"
                            ]];
        [self setAllKeys:@[
                            @"province",@"city",@"area",@"addr",@"postalcode",@"name",@"mobile"
                            ]];
    }
    return self;
}
@end


@implementation GYMineShopAddressDetailModel
@end


@implementation GYMineShopAddressEditViewModel
#pragma mark - getter and setter
-(GYMineShopAddressUpdateModel *)updateModel
{
    if (!_updateModel) {
        _updateModel = [GYMineShopAddressUpdateModel new];
        if (self.listViewModel.datas.count > 0) {
            _updateModel.defaultflag = @"1";
        }else{
            _updateModel.defaultflag = @"0";
        }
    }
    return _updateModel;
}
-(GYMineShopAddressModel *)currentModel
{
    if (!_currentModel) {
        _currentModel = [GYMineShopAddressModel new];
        if (_indexPath && _listViewModel) {
            _currentModel = _listViewModel.datas[_indexPath.row];
        }
    }
    return _currentModel;
}

-(NSMutableArray *)items
{
    if (!_items) {
        _items = [NSMutableArray new];
    }
    return _items;
}
-(NSMutableArray *)cellActions
{
    if (!_cellActions) {
        _cellActions = @[@"chooseStartArea:"].mutableCopy;
    }
    return _cellActions;
}
#pragma mark - method
-(void)reloadItems
{
    NSMutableArray *arr = [NSMutableArray new];
    
    GCBaseEditCellViewModel *cellmodel1 = [GCBaseEditCellViewModel new];
    cellmodel1.title = @"所在地区";
    NSString *areas = self.detailModel ? [NSString stringWithFormat:@"%@ %@ %@",self.detailModel.provincename,self.detailModel.cityname,self.detailModel.areaname] : @"";
    cellmodel1.value = areas;
    cellmodel1.tapAction = @"chooseStartArea:";
    self.updateModel.province = self.detailModel.province;
    self.updateModel.city = self.detailModel.city;
    self.updateModel.area = self.detailModel.area;
    
    GCBaseEditCellViewModel *cellmodel3 = [GCBaseEditCellViewModel new];
    cellmodel3.title = @"详细地址";
    cellmodel3.modelKey = @"addr";
    cellmodel3.value = self.detailModel.addr;
    self.updateModel.addr = cellmodel3.value;
    
    
    GCBaseEditCellViewModel *cellmodel61 = [GCBaseEditCellViewModel new];
    cellmodel61.title = @"收货人姓名";
    cellmodel61.modelKey = @"name";
    cellmodel61.value = self.detailModel.name ? self.detailModel.name : @"";
    cellmodel61.inputType = UIKeyboardTypeDefault;
    self.updateModel.name = cellmodel61.value;
    
    GCBaseEditCellViewModel *cellmodel10 = [GCBaseEditCellViewModel new];
    cellmodel10.title = @"联系电话";
    cellmodel10.modelKey = @"mobile";
    cellmodel10.value = self.detailModel.name ? self.detailModel.mobile : @"";
    cellmodel10.inputType = UIKeyboardTypePhonePad;
    self.updateModel.mobile = cellmodel10.value;
    
    [arr addObject:cellmodel1];
    [arr addObject:cellmodel3];
    [arr addObject:cellmodel61];
    [arr addObject:cellmodel10];
    
    [self setItems:arr];
}

-(void)requestData
{
    if (self.indexPath) {
        @WeakSelf;
        NSString *interface = @"userAddressDetail";
        NSMutableDictionary *para = [NSMutableDictionary new];
        NSString *idd = [NSString stringWithFormat:@"%ld",self.currentModel.id];
        [para setObject:idd forKey:@"id"];
        [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
            NSDictionary *data = infoModel.data;
            if ([data isKindOfClass:[NSDictionary class]]) {
                GYMineShopAddressDetailModel *detail = [GYMineShopAddressDetailModel yy_modelWithJSON:data];
                selfp.updateModel.defaultflag = detail.defaultflag;
                [selfp setDetailModel:detail];
                [selfp reloadItems];
            }
        }];
    }else{
        [self reloadItems];
    }
}

-(void)requestSave
{
    
//    name =request.args.get('name')
//    phone = request.args.get('phone')
//    address = request.args.get('address')
//    province = request.args.get('province')
//    userid = request.args.get('userid')
    @WeakSelf;
    NSString *interface = @"addWineAddress";
    
    NSMutableDictionary *para =[NSMutableDictionary new];
    if (self.detailModel) {
        NSString *idd = [NSString stringWithFormat:@"%ld",self.detailModel.id];
        [para setObject:idd forKey:@"id"];
    }
    [para setObject:self.updateModel.name forKey:@"name"];
    [para setObject:self.updateModel.mobile forKey:@"phone"];
    [para setObject:self.updateModel.addr forKey:@"address"];
    [para setObject:[NSString stringWithFormat:@"%@%@%@",self.updateModel.province,self.updateModel.city,self.updateModel.area] forKey:@"province"];
    [para setObject:@"1" forKey:@"userid"];
    if (para) {
        [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
            [FTIndicator showSuccessWithMessage:@"保存成功"];
            [selfp.listViewModel requestDatas];
            [selfp setUpdateSign:@"1"];
        }];
    }
}
-(void)requestSetDefault
{
    @WeakSelf;
    NSString *interface = @"userAddressSetDefault";
    NSMutableDictionary *para = [NSMutableDictionary new];
    NSString *idd = [NSString stringWithFormat:@"%ld",self.detailModel.id];
    [para setObject:idd forKey:@"id"];
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [FTIndicator showSuccessWithMessage:@"设置成功"];
        [selfp.listViewModel requestDatas];
        selfp.detailModel.defaultflag = @"0";
        selfp.updateModel.defaultflag = @"0";
        NSMutableArray *items = selfp.items;
        [selfp setItems:items];
    }];
}
@end
